package com.cykj.controller;

import com.cykj.service.UserService;
import com.cykj.util.ImageCodeUtils;
import com.cykj.util.ResponseDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    private static String rightCode;
    @Autowired
    private UserService userService;
    @RequestMapping("/deleteUser")
    public ResponseDto deleteUser(int userId) {
        return userService.deleteUser(userId);
    }
    @RequestMapping("/selectAgeCount")
    public ResponseDto selectAgeCount(){
        // 0,10,24,40,60,80,100
        String ages = "0,10,24,40,60,80,100";
        String[] split = ages.split(",");
        List<Integer> agesList = new ArrayList<>();
        for (String ageStr : split) {
            try {
                int age = Integer.parseInt(ageStr.trim()); // 将字符串转换为整数，并去除可能存在的空格
                agesList.add(age); // 将整数添加到列表中
            } catch (NumberFormatException e) {
                // 处理无法转换为整数的情况，例如输入包含非数字字符
                // 可以选择记录错误、抛出异常或忽略错误的输入
                System.err.println("Invalid number format: " + ageStr);
            }
        }
        System.out.println(agesList);
        return userService.selectAgeCount(agesList);
    }
    @RequestMapping("/selectByData")
    public ResponseDto selectByData(int pageCurr, int pageSize,String value) {
        return userService.selectByData(pageCurr, pageSize, value);
    }
    @RequestMapping("/login")
    public ResponseDto login(String name, String pwd,String code) {
        if (code.equals(rightCode)) {
            return userService.login(name, pwd);        } else {
            return new ResponseDto(2, "验证码错误", null);
        }

    }
    @RequestMapping("/register")
    public ResponseDto register(String name,String pwd,String age,String sex,String code){
        // 先判断验证码是否正确，然后再进行账号密码的判断
        if (code.equals(rightCode)) {
            return userService.register(name, pwd,age,sex);
        } else {
            return new ResponseDto(2, "验证码错误", null);
        }

    }
    @RequestMapping("/createCode")
    public void createCode(HttpServletResponse response, HttpSession session) {
        // 生成验证码图片的工具类
        ImageCodeUtils imageCodeUtils = new ImageCodeUtils();
        rightCode = imageCodeUtils.getCode();
        System.out.println("rightCode1"+rightCode);
        try {
            imageCodeUtils.write(response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    @RequestMapping("/alterPwd")
    public ResponseDto alterPwd(int id, String newPwd) {
        return userService.alterPwd(id, newPwd);
    }
}
